package com.joker.product.service.impl;


import com.joker.product.domain.PmsSkuImages;
import com.joker.product.domain.PmsSkuInfo;
import com.joker.product.domain.PmsSpuInfoDesc;
import com.joker.product.service.*;
import com.joker.product.vo.AttrGroupVo;
import com.joker.product.vo.ItemInfoVo;
import com.joker.product.vo.SaleAttrVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemInfoServiceImpl implements ItemInfoService {
    @Resource
    private IPmsSkuInfoService pmsSkuInfoService;
    @Resource
    private IPmsSkuSaleAttrValueService pmsSkuSaleAttrValueService;
    @Resource
    private IPmsAttrGroupService pmsAttrGroupService;
    @Resource
    private IPmsSkuImagesService pmsSkuImagesService;
    @Resource
    private IPmsSpuInfoDescService pmsSpuInfoDescService;
    @Resource
    private ThreadPoolExecutor itemThreadPoolExcuter;

    @Override
    public ItemInfoVo getItemInfoVo(Long skuId) throws ExecutionException, InterruptedException {
        ItemInfoVo itemInfoVo = new ItemInfoVo();

        //         * 1.查sku详情   0.5s
        CompletableFuture<PmsSkuInfo> pmsSkuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            PmsSkuInfo pmsSkuInfo = pmsSkuInfoService.selectPmsSkuInfoBySkuId(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        }, itemThreadPoolExcuter);


        //             * 2.查sku图片信息 1s
        CompletableFuture<Void> skuImagesCompletableFuture = CompletableFuture.runAsync(() -> {
            List<PmsSkuImages> pmsSkuImagesList = pmsSkuImagesService.getPmsSkuImagesList(skuId);
            itemInfoVo.setSkuImagesList(pmsSkuImagesList);
        }, itemThreadPoolExcuter);


        //             * 3.查spu相关信息销售属性 1s
        CompletableFuture<Void> skuSaleAttrCompletableFuture = CompletableFuture.runAsync(() -> {
            List<SaleAttrVo> saleAttrVos = pmsSkuSaleAttrValueService.selectSaleAttrsBySkuId(skuId);
            itemInfoVo.setSaleAttrs(saleAttrVos);
        }, itemThreadPoolExcuter);


        //             * 4.查spu商品介绍 1s
        CompletableFuture<Void> spuInfoDescCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((pmsSkuInfo) -> {
            PmsSpuInfoDesc pmsSpuInfoDesc = pmsSpuInfoDescService.selectPmsSpuInfoDescBySpuId(pmsSkuInfo.getSpuId());
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);
        }, itemThreadPoolExcuter);


        //             * 5.查规格和包装 05s
        CompletableFuture<Void> attrGroupCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync(pmsSkuInfo -> {
            List<AttrGroupVo> attrGroupVos = pmsAttrGroupService.selectAttrGroupVoList(pmsSkuInfo.getCatalogId());
            itemInfoVo.setAttrGroupVos(attrGroupVos);
        }, itemThreadPoolExcuter);
        //阻塞
        CompletableFuture.allOf(
                pmsSkuInfoCompletableFuture,
                skuImagesCompletableFuture,
                skuSaleAttrCompletableFuture,
                spuInfoDescCompletableFuture,
                attrGroupCompletableFuture
                ).get();

        return itemInfoVo;
    }
}
